<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%news_category}}".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $name
 * @property string $icon
 * @property integer $order
 * @property integer $status
 * @property integer $version
 * @property string $created_by
 * @property integer $created_at
 * @property string $created_from
 * @property string $created_ip
 * @property string $updated_by
 * @property integer $updated_at
 * @property string $updated_from
 * @property string $updated_ip
 * @property integer $is_deleted
 */
class NewsCategory extends \common\base\BaseActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%news_category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent_id', 'order', 'status', 'version', 'created_at', 'updated_at', 'is_deleted'], 'integer'],
            [['name'], 'required'],
            [['name', 'icon'], 'string', 'max' => 100],
            [['created_by', 'created_from', 'created_ip', 'updated_by', 'updated_from', 'updated_ip'], 'string', 'max' => 50],
            ['order', 'default', 'value' => 0],
            ['status', 'in', 'range' => [self::STATUS_FLAG_TEMP, self::STATUS_FLAG_NORMAL, self::STATUS_FLAG_STOP]],
            ['is_deleted', 'default', 'value' => self::DELETE_FLAG_NO],
            ['is_deleted', 'in', 'range' => [self::DELETE_FLAG_NO, self::DELETE_FLAG_YES]],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'id'),
            'parent_id' => Yii::t('common', 'parent_id'),
            'name' => Yii::t('common', 'name'),
            'icon' => Yii::t('common', 'icon'),
            'order' => Yii::t('common', 'order'),
            'status' => Yii::t('common', 'status'),
            'version' => Yii::t('common', 'version'),
            'created_by' => Yii::t('common', 'created_by'),
            'created_at' => Yii::t('common', 'created_at'),
            'created_from' => Yii::t('common', 'created_from'),
            'created_ip' => Yii::t('common', 'created_ip'),
            'updated_by' => Yii::t('common', 'updated_by'),
            'updated_at' => Yii::t('common', 'updated_at'),
            'updated_from' => Yii::t('common', 'updated_from'),
            'updated_ip' => Yii::t('common', 'updated_ip'),
            'is_deleted' => Yii::t('common', 'is_deleted'),
        ];
    }

    public static function getNewsCategoryName($parentId = null){
        if (empty($parentId)) return null;
        $model = NewsCategory::findOne($parentId);
        return $model ? $model->name : null;
    }
}
